package com.cloud.order.vo;

import com.cloud.order.enums.ResponseCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

import java.io.Serializable;

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ResultVO<T> implements Serializable {

    private Integer code;

    private T data;

    private String message;

    public ResultVO(Integer code) {
        this.code = code;
    }

    public ResultVO(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResultVO(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code.equals(ResponseCode.SUCCESS.getCode());
    }

    /*----------------------------------成功返回----------------------------------------------*/

    public static <T> ResultVO<T> createBySuccess() {
        return new ResultVO<>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ResultVO<T> createBySuccessMessage(String msg) {
        return new ResultVO<>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ResultVO<T> createBySuccess(T data) {
        return new ResultVO<>(ResponseCode.SUCCESS.getCode(),data,"SUCCESS");
    }

    public static <T> ResultVO<T> createBySuccess(String msg, T data) {
        return new ResultVO<T>(ResponseCode.SUCCESS.getCode(),data,msg);
    }

    /*----------------------------------失败返回----------------------------------------------*/

    public static <T> ResultVO<T> createByError() {
        return new ResultVO<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ResultVO<T> createByError(ResponseCode responseCode) {
        return new ResultVO<T>(responseCode.getCode(),responseCode.getDesc());
    }

    public static <T> ResultVO<T> createByErrorMessage(String msg) {
        return new ResultVO<>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T> ResultVO<T> createByError(T data) {
        return new ResultVO<>(ResponseCode.ERROR.getCode(),data);
    }

    public static <T> ResultVO<T> createByError(String msg, T data) {
        return new ResultVO<T>(ResponseCode.ERROR.getCode(),data,msg);
    }

}
